package com.summer.springbootorderrabbitmqproducer.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class orderService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public void makeOrder(String userId,String productId,int num){
        //1.根据商品id查询库存是否充足
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生产成功:"+orderId);
        //3.通过MQ完成消息的分发
        //参数1:交换机 参数2:路由key/queue队列名 参数3:消息内容
        String exchangeName ="fanout_order_exchange";
        String routingKey = "";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);

    }
    public void makeOrderDirect(String userId,String productId,int num){
        //1.根据商品id查询库存是否充足
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生产成功:"+orderId);
        //3.通过MQ完成消息的分发
        //参数1:交换机 参数2:路由key/queue队列名 参数3:消息内容
        String exchangeName ="direct_order_exchange";
        rabbitTemplate.convertAndSend(exchangeName,"email",orderId);
        rabbitTemplate.convertAndSend(exchangeName,"sms",orderId);

    }
    public void makeOrderTopic(String userId,String productId,int num){
        //1.根据商品id查询库存是否充足
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生产成功:"+orderId);
        //3.通过MQ完成消息的分发
        //参数1:交换机 参数2:路由key/queue队列名 参数3:消息内容
        String exchangeName ="topic_order_exchange";
        String routingkey = "com.duanxin";
        rabbitTemplate.convertAndSend(exchangeName,routingkey,orderId);

    }
    public void makeOrderTTL(String userId,String productId,int num){
        //1.根据商品id查询库存是否充足
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生产成功:"+orderId);
        //3.通过MQ完成消息的分发
        //参数1:交换机 参数2:路由key/queue队列名 参数3:消息内容
        String exchangeName ="ttl_direct_order_exchange";
        String routingkey = "ttl";
        rabbitTemplate.convertAndSend(exchangeName,routingkey,orderId);

    }
    public void makeOrderTTLMessage(String userId,String productId,int num){
        //1.根据商品id查询库存是否充足
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生产成功:"+orderId);
        //3.通过MQ完成消息的分发
        //参数1:交换机 参数2:路由key/queue队列名 参数3:消息内容
        String exchangeName ="ttl_direct_order_exchange";
        String routingkey = "ttlmessage";
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("5000");
                message.getMessageProperties().setContentEncoding("UTF-8");
                return message;
            }
        };
        rabbitTemplate.convertAndSend(exchangeName,routingkey,orderId,messagePostProcessor);

    }
}
